package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	fmt.Println("打开数据")
	db, err := sql.Open("sqlite3", "./userDB.db")//若数据库没有在这个项目文件下，则需要写绝对路径
	checkErr(err)

	fmt.Println("生成数据表")
	//数据表除了在命令行提前进行创建，还可以在go程序中创建，如下：
	sql_table := `
CREATE TABLE IF NOT EXISTS "userinfo" (
   "uid" INTEGER PRIMARY KEY AUTOINCREMENT,
   "username" VARCHAR(64) NULL,
   "departname" VARCHAR(64) NULL,
   "created" TIMESTAMP default (datetime('now', 'localtime'))
);
CREATE TABLE IF NOT EXISTS "userdeatail" (
   "uid" INT(10) NULL,
   "intro" TEXT NULL,
   "profile" TEXT NULL,
   PRIMARY KEY (uid)
);`
	//以上``内不能有任何注释.否则被执行,然后报错！
	_ ,err = db.Exec(sql_table)//执行数据表
	if err != nil {
		fmt.Println(err)
	}

	//插入数据
	fmt.Print("插入数据, ID=")
	stmt, err := db.Prepare("INSERT INTO userinfo(username, departname)  values(?, ?)")
	checkErr(err)
	res, err := stmt.Exec("astaxie", "研发部门")
	checkErr(err)
	id, err := res.LastInsertId()//返回新增的id号
	checkErr(err)
	fmt.Println(id)

	//更新数据
	fmt.Println("更新数据 ")
	stmt, err = db.Prepare("update userinfo set username=? where uid=?")
	checkErr(err)
	res, err = stmt.Exec("astaxieupdate", id)//将新增的id的username修改为astaxieupdate
	checkErr(err)
	affect, err := res.RowsAffected()
	checkErr(err)
	fmt.Println(affect,"条数据被更新")

	//查询数据
	fmt.Println("查询数据")
	rows, err := db.Query("SELECT * FROM userinfo")
	checkErr(err)
	for rows.Next() {
		var uid int
		var username string
		var department string
		var created string
		err = rows.Scan(&uid, &username, &department, &created)
		checkErr(err)
		fmt.Println(uid, username, department, created)
	}

	//删除数据；
	fmt.Print("删除数据,ID=")
	stmt, err = db.Prepare("delete from userinfo where uid=?")
	checkErr(err)
	res, err = stmt.Exec(id)//将想删除的id输入进去就可以删除输入的id
	checkErr(err)
	affect, err = res.RowsAffected()//几条数据受影响：返回int64类型数据
	checkErr(err)
	fmt.Println(id)
	fmt.Println(affect,"条数据被删除")

	//批量删除数据
	IDList:=[]int{3,4}
	for _,id :=range IDList{
		fmt.Println("删除数据,ID=")
		stmt, err = db.Prepare("delete from userinfo where uid=?")
		checkErr(err)
		res, err = stmt.Exec(id)//将想删除的id输入进去就可以删除输入的id
		checkErr(err)
		affect, err = res.RowsAffected()//几条数据受影响：返回int64类型数据
		checkErr(err)
		fmt.Println(id)
		fmt.Println(affect,"条数据被删除")
	}


	//查询数据:上面进行了删除新增的数据，所以并不知道当前数据有几条，再查询一次。
	fmt.Println("查询数据")
	rows, err = db.Query("SELECT * FROM userinfo")
	checkErr(err)
	for rows.Next() {
		var uid int
		var username string
		var department string
		var created string
		err = rows.Scan(&uid, &username, &department, &created)
		checkErr(err)
		fmt.Println(uid, username, department, created)
	}
	db.Close()
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

